//
//  CardScrollVC.swift
//  SwiftDemo
//
//  Created by cqf on 2025/10/10.
//

import UIKit

final class CardScrollVC: UIViewController {

    private let scrollView = UIScrollView()
    private let stack      = UIStackView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground

        scrollView.alwaysBounceVertical = true
        stack.axis = .vertical
        stack.spacing = 12

        view.addSubview(scrollView)
        scrollView.addSubview(stack)

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        stack.translatesAutoresizingMaskIntoConstraints      = false

        // scrollView 钉四周
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        // stack 四边贴 contentLayoutGuide，宽度再贴 frameLayoutGuide
        NSLayoutConstraint.activate([
            stack.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 16),
            stack.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 16),
            stack.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -16),
            stack.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -16),
            stack.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor, constant: -32)
        ])

        // 随便扔点卡片
//        for i in 0..<20 {
//            let card = UIView()
//            card.backgroundColor = .red
//            card.layer.cornerRadius = 8
//            stack.addArrangedSubview(card)
//            card.heightAnchor.constraint(equalToConstant: CGFloat(80 + i % 3 * 20)).isActive = true
//        }
        
        // 1. 直接创建我们自定义的 BannerView
           let banner = BannerView(
               image: UIImage(named: "baogao"),
               cornerRadius: 12
           )
        stack.addSubview(banner)
        
        // 3. 给 banner 做布局（居中 + 固定宽高）
        banner.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            banner.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            banner.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
            banner.widthAnchor.constraint(equalToConstant: 300),
            banner.heightAnchor.constraint(equalToConstant: 160)
        ])
        
    }
    
    
    /// 1. 完全自包含的 UIView，内部自己放 UIImageView
    final class BannerView: UIView {

        private let imageView = UIImageView()

        // 2. 对外只暴露这一种初始化方式
        convenience init(image: UIImage?, cornerRadius: CGFloat = 0) {
            self.init(frame: .zero)
            imageView.image = image
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            imageView.layer.cornerRadius = cornerRadius
            commonInit()
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
        }

        @available(*, unavailable)
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        // 3. 统一布局，完全内部搞定
        private func commonInit() {
            addSubview(imageView)
            imageView.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                imageView.topAnchor.constraint(equalTo: topAnchor),
                imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
                imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
                imageView.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])
        }
    }
    
    
}
